package cn.edu.ctbu.scoremg.service;

import cn.edu.ctbu.scoremg.dao.ScoreRepository;
import cn.edu.ctbu.scoremg.entity.Score;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
public class ScoreService {
    @Autowired
    public ScoreRepository scoreRepository;
    public List<Score> getAll(){
        return scoreRepository.findAll();
    }

    /**
     * 增
     * @param score
     * @return
     */
    public Score add(Score score){
        return scoreRepository.save(score);
    }

    /**
     * 查
     * @param id
     * @return
     */
    public Score findById(Integer id){
        return scoreRepository.findById(id).orElse(null);
    }

    /**
     * 修改
     * @param score
     * @return
     */
    public Score update(Score score){
        return scoreRepository.save(score);
    }

    /**
     * 删
     * @param id
     */
    public void delete(Integer id){
        scoreRepository.deleteById(id);
    }

    /**
     * 删多个
     * @param ids
     */
    @Transactional
    public void deleteByIds(List<Integer> ids){

        scoreRepository.deleteByIds(ids);

    }

    /**
     * 分页
     * @param pageable
     * @return
     */
    public Page<Score> getAll(Pageable pageable){
        return scoreRepository.findAll(pageable);
    }
    public Page<Score> getAll(Example<Score> example, Pageable pageable){
        return scoreRepository.findAll(example,pageable);
    }
}
